requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {
    Constant.initHandlebars(Handlebars);
    Public.initApplyTerm();
    var url = {
        queryOrder: 'manager/queryOrderRefuse.do',
        activOrder: 'manager/activOrder.do',
        selectLoanMethodList: "manager/selectLoanMethodList.do",
    }
    var userMessage = {
        roleId: JSON.parse(localStorage.getItem('content')).admin.roleid,
        userId: JSON.parse(localStorage.getItem('content')).admin.id
    }
    Public.initProductSearch();
    //查询产品所需字段
    var frontData = {
        roleId: userMessage.roleId,
        pageNum: 1
    };
    var order_id = "";
    var product_id;
    var repayment_way;

    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('#loan_way').append(html);
            }
        }
    })

    function salesManCheck() {
        var data = {
            roleId: userMessage.roleId,
            audit_status: getAurlparam("t"),
            pageNum: frontData.pageNum,
            susp_status: '2'
        }
        if ($("#start_time").val()) {
            data.beginTime = $("#start_time").val()
        }
        if ($("#end_time").val()) {
            data.endTime = $("#end_time").val()
        }
        if ($("#seleNames option:selected").val()) {
            data.type = $("#seleNames option:selected").val()
        }
        if ($("#inputNames").val()) {
            data.keyword = $("#inputNames").val();
        }
        if ($("#at_id option:selected").val()) {
            data.atId = $("#at_id option:selected").val()
        }
        if ($("#loan_way option:selected").val()) {
            data.loan_way = $("#loan_way option:selected").val()
        }
        if ($("#product_choose_list").val()) {
            data.product_id = $("#product_choose_list").val()
        }
        site.send({
            url: url.queryOrder,
            data: data,
            callback: function (data) {
                if (data && data !== "noLogin") {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        if (data.content) {
                            $('.refuse-page-container').createPager({
                                current: frontData.pageNum,
                                total: data.content.total,
                                pageSize: data.content.pageSize,
                                needInput: false,
                                backFn: function (current) {
                                    frontData.pageNum = current;
                                    salesManCheck();
                                }
                            })
                            $('.result').html(data.content.total ? data.content.total : 0);
                        }
                        //handlebars模板引擎
                        var source = $("#refuseorder_content_tpl").html()
                        var template = Handlebars.compile(source)
                        var content = data.content;

                        var menuIndex = getAurlparam('h') || 0;
                        var thirdMenusIndex = getAurlparam('t') || 0;
                        var leftIndex = getAurlparam('i') || 0;

                        var storageContent = JSON.parse(localStorage.getItem('content'));

                        var obj = {
                            content: content,
                            fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                        }
                        var html = template(obj)
                        $('#refuseorder_content').html(html)

                        //复选框一个没选中则全选去除
                        $.each($("input[name='ch_id']"), function (i, e) {
                            var flag = false;
                            $(e).on('click', function () {
                                var _checked = $("input[name='ch_id_all']")
                                var _checkedAll = $("input[name='ch_id']")
                                $.each(_checkedAll, function (ii, ee) {
                                    if ($(ee).prop('checked')) {
                                        flag = true
                                    }
                                    else {
                                        flag = false
                                        return false;
                                    }
                                })
                                _checked.prop('checked', flag);
                            })
                        })

                        //渲染出模板后在进行操作
                        $(".operations").on('change', function () {
                            product_id = $(this).attr('product_id');
                            order_id = $(this).attr('order_id');
                            taskId = $(this).attr('task_id');
                            node_type = $(this).attr('node_type')
                            node_id = $(this).attr('node_id')
                            s_id = $(this).attr('s_id')
                            repayment_way = $(this).attr('repayment_way');
                            if ($(this).val() == '编辑订单') {
                                pjaxGo("loanBefore/orderEdit.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id);
                            }
                            else if ($(this).val() == 'checkOrder') {
                                pjaxGo("loanBefore/orderDetail.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id);
                            }
                            else if ($(this).val() == 'activeOrder') {
                                //order_id = $(this).attr('order_id');
                                $('.cover_winBox').toggle();
                                $('.coverRemarks').val('');
                            }
                            $(this).find("option:first").attr("selected", false).attr("selected", true);
                        });

                        Public.record('t_order_public');

                    } else {
                        Constant.fn.Message("F", data.msg);
                    }
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }

    salesManCheck();


    /*var template=Handlebars.compile($("#refuseorder_content_tpl").html())
    var html=template(data)
    $("#refuseorder_content").html(html)
    Public.record('t_doc')*/

    // }
    // })
    //日期控件
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    })
    //头部日期
    // site.send({
    //     url: "",
    //     //async:false
    //     data: {},
    //     callback: function (data) {
    //     var list = JSON.parse(data)


    /*var template2 = Handlebars.compile($("#refuseorder_head_date_tpl").html())
    var html2 = template2(data2)
    $("#refuseorder_head_date").html(html2)*/
    // }
    // })


    //拿到所有的复选框，当有复选框没选上的时候,取消全选
    $("input:checkbox[name='ch_id']").on('click', function () {
        var checkboxs = $("input:checkbox[name='ch_id']");
        var sum = 0;
        var length = checkboxs.length;
        $.each(checkboxs, function (i, checkbox) {
            if ($(checkbox).prop("checked")) {
                sum++;
            }
        })
        if (sum == length) {
            $('.all_checked').prop("checked", true);
        } else {
            $('.all_checked').prop("checked", false);
        }
    })
    // 全选
    $(".all_checked").click(function () {
        $("input[name='ch_id']").prop('checked', $(this).prop('checked'))
    })


    $('.sureCover').on('click', function () {
        var remarks = $('.coverRemarks').val();
        if (!remarks) {
            Constant.fn.Message('I', '请输入备注')
            return;
        }
        site.send({
            url: url.activOrder,
            data: {
                oid: order_id,
                remarks: remarks
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    $('.cover_winBox').toggle();
                    Constant.fn.Message("T", data.msg);
                    pjaxReload();
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    })

    //批量操作恢复订单select
    $("#allDown_select").on('change', function () {
        /*var obj = $(this).find("option:selected");
        // 下载合同
        if (obj.val() == '2') {
            allDownLoadData.oid = '';
            var inp = $("input[name='ch_id']");//获得所有check按钮
            var count = 0;
            for (var i = 0; i < inp.length; i++) {
                if (inp[i].checked) {
                    count++;
                    allDownLoadData.oid = inp[i].value;
                    allDownLoad();
                }
                if (count == 0) {
                    Constant.fn.Message("I", "未选择")
                }
            }

            $("#allDown_select").val('-1')

        }*/
        order_id = "";

        //console.log(1);
        var selected = $(this).find("option:selected");
        $(this).find("option:first").prop("selected", false).prop("selected", true);
        $("input[name='ch_id']:checked").each(function (index, item) {
            order_id = order_id + $(item).val() + ","
        })
        order_id = order_id.substring(0, order_id.length - 1)
        if (selected.val() == '恢复订单') {
            //删除产品
            if (!order_id) {
                Constant.fn.Message('I', "未选择要恢复的订单")
                return;
            }
            $('.cover_winBox').toggle();
        }
    })

    //搜索按钮
    $("#sousuo").on('click', function () {
        salesManCheck();
    })

    $(".canCover").on('click', function () {
        $(this).parents('.winBox').hide();
    })
})